package org.hiucung.orangeppjoke.ui.publish;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import org.hiucung.libcommon.utils.PixUtils;
import org.hiucung.libnetwork.ApiResponse;
import org.hiucung.libnetwork.ApiService;
import org.hiucung.libnetwork.JsonCallback;
import org.hiucung.orangeppjoke.R;
import org.hiucung.orangeppjoke.model.PublishType;
import org.hiucung.orangeppjoke.model.TagList;
import org.hiucung.orangeppjoke.ui.login.UserManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kicc
 * @title: TypeBottomSheetDialogFragment
 * @projectName OrangePPJoke
 * @description: TODO
 * @date 2022/1/27 21:58
 */
public class TypeBottomSheetDialogFragment extends BottomSheetDialogFragment {
    private RecyclerView recyclerView;
    private TypeBottomSheetDialogFragment.TypesAdapter typesAdapter;
    private List<PublishType> mTypeList = new ArrayList<>();
    private TypeBottomSheetDialogFragment.OnTypeItemSelectedListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_type_bottom_sheet_dialog, null, false);
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        typesAdapter = new TypeBottomSheetDialogFragment.TypesAdapter();
        recyclerView.setAdapter(typesAdapter);

        dialog.setContentView(view);
        ViewGroup parent = (ViewGroup) view.getParent();
        BottomSheetBehavior<ViewGroup> behavior = BottomSheetBehavior.from(parent);
        behavior.setPeekHeight(PixUtils.getScreenHeight() / 3);
        behavior.setHideable(false);


        ViewGroup.LayoutParams layoutParams = parent.getLayoutParams();
        layoutParams.height = PixUtils.getScreenHeight() / 3 * 2;
        parent.setLayoutParams(layoutParams);

        addTypeList();
        return dialog;
    }

    @SuppressLint("RestrictedApi")
    private void addTypeList() {
        PublishType capture = new PublishType(1,"拍摄上传");
        PublishType file = new PublishType(2,"文件上传");
        mTypeList.add(capture);
        mTypeList.add(file);
        ArchTaskExecutor.getMainThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                typesAdapter.notifyDataSetChanged();
            }
        });
    }

    public void setOnTypeItemSelectedListener(TypeBottomSheetDialogFragment.OnTypeItemSelectedListener listener) {
        this.listener = listener;
    }


    class TypesAdapter extends RecyclerView.Adapter {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            TextView textView = new TextView(parent.getContext());
            textView.setTextSize(16);
            textView.setTextColor(ContextCompat.getColor(parent.getContext(), R.color.color_000));
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(new RecyclerView.LayoutParams(-1, PixUtils.dp2px(45)));

            return new RecyclerView.ViewHolder(textView) {
            };
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            TextView textView = (TextView) holder.itemView;
            PublishType publishType = mTypeList.get(position);
            textView.setText(publishType.type);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onTypeItemSelected(publishType);
                        dismiss();
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mTypeList.size();
        }
    }

    public interface OnTypeItemSelectedListener {
        void onTypeItemSelected(PublishType publishType);
    }
}
